"""
Case Type   : ROLE
Case Name   : 创建角色，添加auditadmin，测试角色是否具有opradmin权限
Create At   : 2024-04-30
Owner       : liu-tong-8848
Description :
    1、创建角色r_role_0015_1，添加auditadmin
    2、通过系统表查询r_role_0015_1是否为管理员
    3、创建角色r_role_0015_2，添加noauditadmin
    4、通过系统表查询r_role_0015_2是否为管理员
    5、清理环境
Expect      :
    1、成功
    2、成功，r_role_0015_1是管理员
    3、成功
    4、成功，r_role_0015_2不是管理员
    5、成功
History     :
"""

import os
import unittest

from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Purge(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.r_name1 = 'r_role_0015_1'
        self.r_name2 = 'r_role_0015_2'

    def test_main(self):
        step1_txt = '----step1: 创建角色1，expect: 成功----'
        self.log.info(step1_txt)
        sql_cmd1 = f"drop role if exists {self.r_name1};" \
                   f"create role {self.r_name1} opradmin " \
                   f"password '{macro.COMMON_PASSWD}';"
        msg1 = self.pri_sh.execut_db_sql(sql_cmd1)
        self.log.info(msg1)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, msg1,
                      '执行失败:' + step1_txt)

        step2_txt = '----step2:通过系统表查询角色1是否为管理员;expect:成功----'
        self.log.info(step2_txt)
        sql_cmd2 = f"select rolname,roloperatoradmin from pg_authid " \
                   f"where rolname = '{self.r_name1}';"
        msg2 = self.pri_sh.execut_db_sql(sql_cmd2)
        self.log.info(msg2)
        self.assertIn(f"{self.r_name1} | t", msg2, '执行失败:' + step2_txt)

        step3_txt = '----step3: 创建角色2，expect: 成功----'
        self.log.info(step3_txt)
        sql_cmd3 = f"drop role if exists {self.r_name2};" \
                   f"create role {self.r_name2} with noopradmin " \
                   f"password '{macro.COMMON_PASSWD}';"
        msg3 = self.pri_sh.execut_db_sql(sql_cmd3)
        self.log.info(msg3)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, msg3,
                      '执行失败:' + step3_txt)

        step4_txt = '----step4:通过系统表查询角色2是否为管理员，expect: 成功----'
        self.log.info(step4_txt)
        sql_cmd4 = f"select rolname,roloperatoradmin from pg_authid " \
                   f"where rolname = '{self.r_name2}';"
        msg4 = self.pri_sh.execut_db_sql(sql_cmd4)
        self.log.info(msg4)
        self.assertIn(f"{self.r_name2} | f", msg4, '执行失败:' + step4_txt)

    def tearDown(self):
        self.log.info('----this is teardown----')
        step5_txt = '----step5: 清理环境----'
        self.log.info(step5_txt)
        drop_sql = f'drop role if exists {self.r_name1}, {self.r_name2};'
        drop_result = self.pri_sh.execut_db_sql(drop_sql)
        self.log.info(drop_result)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, drop_result,
                      '执行失败:' + step5_txt)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
